<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>复选框全选功能</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            background-color: rgba(255, 255, 255, 0.92);
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 500px;
            padding: 30px;
            transition: transform 0.3s ease;
        }
        
        .container:hover {
            transform: translateY(-5px);
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.2rem;
            font-weight: 600;
        }
        
        .subtitle {
            text-align: center;
            color: #7f8c8d;
            margin-bottom: 30px;
            font-size: 1rem;
        }
        
        .checkbox-group {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }
        
        .checkbox-item {
            display: flex;
            align-items: center;
            padding: 16px 20px;
            border-bottom: 1px solid #f0f0f0;
            transition: all 0.2s ease;
        }
        
        .checkbox-item:last-child {
            border-bottom: none;
        }
        
        .checkbox-item:hover {
            background-color: #f8f9fa;
        }
        
        .checkbox-item label {
            margin-left: 15px;
            font-size: 1.1rem;
            color: #34495e;
            cursor: pointer;
            flex-grow: 1;
        }
        
        .checkbox-all {
            background-color: #f1f8ff;
            border-bottom: 2px solid #e0e0e0;
            font-weight: 600;
        }
        
        input[type="checkbox"] {
            width: 20px;
            height: 20px;
            cursor: pointer;
            accent-color: #3498db;
        }
        
        .status-bar {
            margin-top: 25px;
            padding: 15px;
            background: #e3f2fd;
            border-radius: 10px;
            text-align: center;
            font-size: 1.1rem;
            color: #2c3e50;
            font-weight: 500;
        }
        
        .explanation {
            margin-top: 30px;
            padding: 20px;
            background: #f9f9f9;
            border-radius: 10px;
            border-left: 4px solid #3498db;
        }
        
        .explanation h3 {
            color: #2c3e50;
            margin-bottom: 12px;
        }
        
        .explanation ul {
            padding-left: 20px;
        }
        
        .explanation li {
            margin-bottom: 8px;
            line-height: 1.5;
        }
        
        .code {
            font-family: 'Courier New', monospace;
            background: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 8px;
            margin-top: 15px;
            font-size: 0.9rem;
            overflow-x: auto;
        }
        
        .fixed {
            color: #2ecc71;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>复选框全选功能</h1>
        <p class="subtitle">修复版 - 点击"全选/全不选"可控制所有选项</p>
        
        <div class="checkbox-group">
            <div class="checkbox-item checkbox-all">
                <input type="checkbox" id="checkbox-all">
                <label for="checkbox-all">全选/全不选</label>
            </div>
            <div class="checkbox-item">
                <input type="checkbox" id="checkbox1" class="checkbox-item">
                <label for="checkbox1">选项1</label>
            </div>
            <div class="checkbox-item">
                <input type="checkbox" id="checkbox2" class="checkbox-item">
                <label for="checkbox2">选项2</label>
            </div>
            <div class="checkbox-item">
                <input type="checkbox" id="checkbox3" class="checkbox-item">
                <label for="checkbox3">选项3</label>
            </div>
            <div class="checkbox-item">
                <input type="checkbox" id="checkbox4" class="checkbox-item">
                <label for="checkbox4">选项4</label>
            </div>
            <div class="checkbox-item">
                <input type="checkbox" id="checkbox5" class="checkbox-item">
                <label for="checkbox5">选项5</label>
            </div>
        </div>
        
        <div class="status-bar">
            <p>当前已选择: <span id="selected-count">0</span> 个项目</p>
        </div>
        
       
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取全选复选框（修正ID）
            const checkAll = document.getElementById('checkbox-all');
            // 获取所有子复选框
            const checkboxes = document.querySelectorAll('.checkbox-item');
            // 获取计数元素
            const selectedCount = document.getElementById('selected-count');
            
            // 更新全选状态和计数
            function updateCheckAll() {
                let checkedCount = 0;
                checkboxes.forEach(checkbox => {
                    if(checkbox.checked) checkedCount++;
                });
                
                selectedCount.textContent = checkedCount;
                checkAll.checked = checkedCount === checkboxes.length;
                checkAll.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
            }
            
            // 全选复选框事件
            checkAll.addEventListener('change', function() {
                checkboxes.forEach(checkbox => {
                    checkbox.checked = checkAll.checked;
                });
                updateCheckAll();
            });
            
            // 为每个子复选框添加事件
            checkboxes.forEach(checkbox => {
                checkbox.addEventListener('change', updateCheckAll);
            });
            
            // 初始更新
            updateCheckAll();
        });
    </script>
</body>
</html>